from flask import Flask
from bluelog.settings import config
import os
from bluelog.blueprints.admin import admin_bp
from bluelog.blueprints.auth import auth_bp
from bluelog.blueprints.blog import blog_bp
from bluelog.extensions import bootstrap, db, moment, ckeditor, mail
from flask import render_template
import click
from bluelog.models import Adimn, Post, Category, Comment

def create_app(config_name=None):
    if config_name is None:
        config_name = os.getenv('FLASK_CONFIG', 'development')

    app = Flask('bluelog')
    app.config.from_object(config[config_name])

    register_login(app)
    register_blueprints(app)
    register_extensions(app)
    register_commands(app)
    register_errors(app)
    register_shell_context(app)
    register_template_contex(app)

    return app

def register_login(app):
    pass

def register_extensions(app):
    bootstrap.init_app(app)
    db.init_app(app)
    moment.init_app(app)
    ckeditor.init_app(app)
    mail.init_app(app)

def register_blueprints(app):
    app.register_blueprint(blog_bp)
    app.register_blueprint(admin_bp, url_prefix='/admin')
    app.register_blueprint(auth_bp, url_prefix='/auth')

def register_shell_context(app):
    @app.shell_context_processor
    def make_shell_context():
        return dict(db=db)

def register_template_contex(app):
    pass

def register_errors(app):
    @app.errorhandler(400)
    def bad_request(e):
        return render_template('errors/400.html'), 400

def register_commands(app):
    # @app.cli.command()
    # @click.option('--drop', is_flage=True, help='Create after drop.')
    # def initdb(drop):
    #     """Initialize the databaser."""
    #     if drop:
    #         click.confirm('This operation will delete the datebase, do you want to continue?', abort=True)
    #         db.drop_all()
    #         click.echo('Drop tables')
    #     db.create_all()
    #     click.echo('Initialized database')

    # @app.cli.command()
    # @click.option('--username', prompt=True, help='The username used to login.')
    # @click.option('--password', prompt=True, hide_input=True, confirmation_prompt=True,  help='The username used to login.')
    #
    # def init(username, password):
    #     """Build Bluelog, just for you."""
    #
    #     click.echo('Initializeing the database...')
    #     db.create_all()
    #
    #     admin = Adimn.query.first()
    #     if admin is not None:
    #         click.echo('The administrator already exists, updating...')
    #         admin.username=username
    #         admin.set
    @app.cli.command()
    @click.option('--category', default=10, help='Quantity of categories, default is 10')
    @click.option('--post', default=50, help='Quantity of post, default is 10')
    @click.option('--comment', default=500, help='Quantity of comment, default is 10')
    def forge(category, post, comment):
        from bluelog.fakes import fake_comments, fake_categories, fake_admin, fake_post
        """生成虚拟数据"""
        db.drop_all()
        db.create_all()

        click.echo('Generating the administrator...')
        fake_admin()

        click.echo('Generating %d administrator...' % category)
        fake_categories(category)

        click.echo('Generating %d administrator...' % post)
        fake_post(post)

        click.echo('Generating %d administrator...' % comment)
        fake_comments(comment)

        click.echo('Done...')





